﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Web.UI.WebControls.WebParts;
using System.ComponentModel;
using System.Web.UI.WebControls;
using VPLink2013.Framework;
using System.Drawing;
using System.Web.UI;

namespace VPLink2013.Webpart
{
    public class Calendar : WebPart
    {

        [WebBrowsable(true)]
        [Personalizable(PersonalizationScope.Shared), WebDescription("ListName"), WebDisplayName("ListName"), Category("Configuration")]
        public string ListName
        {
            get
            {
                return _listName;
            }
            set
            {
                _listName = value;
            }
        }

        [WebBrowsable(true)]
        [Personalizable(PersonalizationScope.Shared), WebDescription("SiteURL"), WebDisplayName("SiteURL"), Category("Configuration")]
        public string SiteUrl
        {
            get
            {
                return _siteUrl;
            }
            set
            {
                _siteUrl = value;
            }
        }

        private FirstDayOfWeek _firstDayOfWeek = FirstDayOfWeek.Default;
        private bool _showCalendar = true;
        private bool _showListing = true;
        private int _numDaysInEventListing = 3;
        private string _cssClassCalendar = "ECal";
        private string _cssClassDay;
        private string _cssClassEvent = "EEvent";
        private string _cssClassEventListing = "EList";
        private string _cssClassHeader;
        private string _cssClassNextPrev = "ENextPrev";
        private string _cssClassOtherMonth = "EOtherMonth";
        private string _cssClassTitle = "ECalTitle";
        private string _cssClassToday = "ECalToday";
        private string _cssClassWeekend;
        private string _listName = String.Empty;
        private string _onMouseOverBackgroundColor = "#FF89DD";
        private string _siteRelativeEventItemUrl;
        private string _siteUrl = String.Empty;
        private string _cssUrl = String.Empty;
        private string _titleBackColor = "#EC008C";

        private void InitEventCalendar(EventCalendar eventCalendar)
        {
            eventCalendar.SiteUrl = _siteUrl.Trim();
            eventCalendar.ListName = _listName;
            eventCalendar.FirstDayOfWeek = _firstDayOfWeek;
            eventCalendar.SiteRelativeEventItemUrl = _siteRelativeEventItemUrl;
            eventCalendar.CssClass = _cssClassCalendar;
            if (!String.IsNullOrEmpty(_titleBackColor))
            {
                try
                {
                    // Calendar CSS bug see http://weblogs.asp.net/vga/archive/2004/02/01/CalendarsBuggyStyleHandling.aspx
                    eventCalendar.TitleStyle.BackColor = ColorTranslator.FromHtml(_titleBackColor);
                }
                catch { }
            }
            eventCalendar.TitleStyle.CssClass = _cssClassTitle;
            eventCalendar.DayHeaderStyle.CssClass = _cssClassHeader;
            eventCalendar.NextPrevStyle.CssClass = _cssClassNextPrev;
            eventCalendar.DayStyle.CssClass = _cssClassDay;
            eventCalendar.SelectedDayStyle.CssClass = _cssClassEvent;
            eventCalendar.TodayDayStyle.CssClass = _cssClassToday;
            eventCalendar.WeekendDayStyle.CssClass = _cssClassWeekend;
            eventCalendar.OtherMonthDayStyle.CssClass = _cssClassOtherMonth;
            eventCalendar.OnMouseOverBackgroundColor = _onMouseOverBackgroundColor;
        }

        protected override void CreateChildControls()
        {
            try
            {
                if (_showCalendar)
                {
                    //css
                    if (String.IsNullOrEmpty(_cssUrl))
                        _cssUrl = SiteUrl + "/_layouts/15/VPLink2013/CSS/calendar.css";
                    this.Controls.Add(new LiteralControl("<link href=\"" + _cssUrl + "\" rel=\"stylesheet\" type=\"text/css\" />"));
                    this.Controls.Add(new LiteralControl("<script type=\"text/javascript\" src=\"/_layouts/15/VPLink2013/js/calendar.js\"></script>"));
                    EventCalendar eventCalendar = new EventCalendar();
                    InitEventCalendar(eventCalendar);
                    this.Controls.Add(eventCalendar);
                }

                if (_showListing)
                {
                    Panel eventListing = new Panel();
                    eventListing.Attributes.Add("rel", "eventListingPanel");
                    eventListing.Attributes.Add("class", "EList");
                    this.Controls.Add(eventListing);
                }
            }
            catch (Exception ex)
            {
                Controls.Add(new LiteralControl(ex.Message));
            }
        }
    }
}
